package com.holly.service.impl;

import com.holly.exception.ServiceException;
import com.holly.mapper.DeptMapper;
import com.holly.mapper.EmpMapper;
import com.holly.pojo.Dept;
import com.holly.service.DeptService;
import com.holly.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author holly
 * @create 2025-06-28 17:39
 */
@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }

    @Override
    public void delete(Integer id) {
        //1.查询该部门下是否有员工
        List<Integer> empByDeptId = empMapper.getEmpByDeptId(id);
        //1.1如果有员工，提醒“对不起, 该部门下有员工, 不能直接删除"
        if(empByDeptId.size() > 0){
            throw new ServiceException("对不起, 该部门下有员工, 不能直接删除");
        }else{
            //1.2没有员工，删除部门
            deptMapper.delete(id);
        }


    }


    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);
    }

    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
